<template>
  <div class="signature-pad">
    <div
      class="signature-wrapper"
      @click="handleOpen"
    >
      <div
        v-if="signImageUrl"
        class="img-wrapper"
      >
        <img
          :src="signImageUrl"
          alt=""
        />
      </div>
      <p v-else>{{ textConfig?.tips }}</p>
    </div>
    <el-button
      v-if="signImageUrl"
      type="primary"
      size="default"
      @click="reSign"
    >
      {{ textConfig?.reSignBtnText }}
    </el-button>
    <van-popup
      v-model:show="visible"
      position="bottom"
      round
      style="padding: 10px"
    >
      <canvas
        ref="signaturePadBoxRef"
        class="canvasId"
        style="border: 2px dashed #409eff"
      />
      <div>
        <el-button
          type="success"
          @click="handleMobileSavePng"
        >
          {{ textConfig?.confirmBtnText }}
        </el-button>
        <el-button
          plain
          @click="undo"
          type="warning"
        >
          {{ textConfig?.backBtnText }}
        </el-button>
        <el-button
          plain
          type="danger"
          @click="clear"
        >
          {{ textConfig?.clearBtnText }}
        </el-button>
      </div>
    </van-popup>
  </div>
</template>

<script setup lang="ts">
import { signPadEmit, signPadProps } from "./signpad";
import { useSignPadHook } from "@/views/formgen/components/FormItem/SignPad/useSignPadHook";
import { ref } from "vue";
const props = defineProps({
  ...signPadProps
});

const emit = defineEmits(signPadEmit);

const { signImageUrl, clear, undo, savePng, reSign, signaturePadBoxRef, initMobileSignaturePad, resizeMobileCanvas } =
  useSignPadHook(props, emit);

const visible = ref(false);

const handleMobileSavePng = () => {
  savePng();
  visible.value = false;
};

const handleOpen = () => {
  visible.value = true;
  setTimeout(() => {
    initMobileSignaturePad();
  }, 500);
};
</script>
<style lang="scss" scoped>
#canvasId {
}
.signature-wrapper {
  background: #f7f8fa;
  border: 1px solid #eaeaea;
  border-radius: 4px;
  width: 100%;
  font-size: 14px;
  height: 250px;
  color: #616161;
  cursor: pointer;
  text-align: center;
  display: flex;
  align-content: center;
  align-items: center;
  justify-content: space-around;

  .img-wrapper {
    height: 100%;
    position: relative;

    img {
      width: 100%;
      height: 80%;
    }

    .re-sign {
      position: absolute;
      bottom: 0;
    }
  }
}
</style>
